﻿/* Filename: Player.as 
 *
 * Description: This class contains the
 * template for the player and the enemies.
 *
 * Tyler Samson
 * Feb 2008
 * @ Sierra Nevada College
 */
 class Player {
	private var _playerName:String;		// player's name
	private var _demons:Array;			// array of demons in the player's possession
	private var _demonsInactive:Array;	// array of active demons in the player's possession
	private var _demonsActive:Array;	// array of active demons in the player's possession
	private var _totalActiveDemons = 3;	// constant number of active demons the player can have
	private var _playerMap:Map;			// the map containing the player's demons
	
	public function Player(p_name:String, p_demons:Array) {
		_playerName = p_name;
		_demons = p_demons;
		
		_demonsActive = new Array(_totalActiveDemons);		// create new array for active demons
		_demonsInactive = new Array();
		
		// Set the owner of each demon to this player
		for (var i = 0; i < p_demons.length; i++) {
			var _nextDemon:Demon = p_demons[i];			// get next demon
			_nextDemon.setOwner(this);					// set the owner to this demon
			
			if (i < _totalActiveDemons) {				// add the first _totalActiveDemons to the list of ActiveDemons
				_demonsActive[i] = _nextDemon;		// place next demon in active demon array
			} else {
				_demonsInactive.push(_nextDemon);
			}
		}
	}
	
	// Return all demons in the player's possession
	public function getAllDemons():Array {
		return this._demons;
	}
	
	// Return the player's active demons - those in a battle
	public function getActiveDemons():Array {
		return this._demonsActive;
	}
	
	// Return the name of this player
	public function getName():String {
		return this._playerName;
	}
	
	// Return the player's map
	public function getMap():Map {
		return this._playerMap;
	}
	
	// Return the player's map
	public function setMap(p_playerMap:Map):Void {
		this._playerMap = p_playerMap;
	}
	
	public function swapDeadDemon(p_demon:Demon):Demon {
		var _returnDemon:Demon = null;
		
		for (var i = 0; i < _demonsInactive.length; i++) {
			if (!_returnDemon && !_demonsInactive[i].isDead()){
				_returnDemon = _demonsInactive[i];
				removeInactiveDemon(_returnDemon);
				_demonsActive.push(_returnDemon);
			}
		}

		return _returnDemon;
	}
	
	public function removeInactiveDemon(p_demon:Demon):Void {
		var _newDemonsInactive:Array = new Array();
		
		for (var i = 0; i < _demonsInactive.length; i++) {
			if (_demonsInactive[i].getUniqueId() != p_demon.getUniqueId()){
				trace(_demonsInactive[i].getName());
				_newDemonsInactive.push(_demonsInactive[i]);
			} else {
				trace("NOT: " + _demonsInactive[i].getName());
			}
		}
		
		_demonsInactive = _newDemonsInactive;
	}
	
	public function removeActiveDemon(p_demon:Demon):Void {
		var _newDemonsActive:Array = new Array();
		
		for (var i = 0; i < _demonsActive.length; i++) {
			if (_demonsActive[i].getUniqueId() != p_demon.getUniqueId()){
				trace(_demonsActive[i].getName());
				_newDemonsActive.push(_demonsActive[i]);
			} else {
				trace("NOT: " + _demonsActive[i].getName());
			}
		}
		
		_demonsActive = _newDemonsActive;
	}
}